﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;


namespace ANZ_AIM_GUI.Utils
{
    public class NPOIStyle
    {
        /*static NPOIStyle instance = null;
      static readonly object padlock = new object();

      NPOIStyle()
      {
      }

      public static NPOIStyle Instance
      {
          get
          {
              lock (padlock)
              {
                  if (instance == null)
                  {
                      instance = new NPOIStyle();
                  }
                  return instance;
              }
          }
      }*/

        private HSSFWorkbook workbook;

        public NPOIStyle()
        {
        }

        public NPOIStyle(HSSFWorkbook workbook)
        {
            this.workbook = workbook;
            this.InitCellStyles(this.workbook);
        }

        public IFont fontHeader;
        public ICellStyle cellstyleHeader;

        public IFont font;
        public IFont fontBold;
        public ICellStyle cellstyle;
        public ICellStyle cellstyleInt;
        public ICellStyle cellstyleIntNoSep;
        public ICellStyle cellstyleDec;
        public ICellStyle cellstyleDate;
        public ICellStyle cellstylePercent;

        public ICellStyle cellstyleHL;
        public ICellStyle cellstyleHLInt;
        public ICellStyle cellstyleHLIntNoSep;
        public ICellStyle cellstyleHLDec;
        public ICellStyle cellstyleHLDate;
        public ICellStyle cellstyleHLPercent;

        public ICellStyle cellstyleHLRed;
        public ICellStyle cellstyleHLRedInt;
        public ICellStyle cellstyleHLRedIntNoSep;
        public ICellStyle cellstyleHLRedDec;
        public ICellStyle cellstyleHLRedDate;
        public ICellStyle cellstyleHLRedPercent;

        public ICellStyle cellstyleHLBold;
        public ICellStyle cellstyleHLBoldInt;
        public ICellStyle cellstyleHLBoldIntNoSep;
        public ICellStyle cellstyleHLBoldDec;
        public ICellStyle cellstyleHLBoldDate;
        public ICellStyle cellstyleHLBoldPercent;

        private void InitCellStyles(HSSFWorkbook workbook)
        {
            // HEADER row
            if (this.fontHeader == null)
            {
                fontHeader = workbook.CreateFont();
                fontHeader.Boldweight = (short)FontBoldWeight.Bold;
                fontHeader.FontName = "Verdana";
                fontHeader.FontHeightInPoints = 10;
            }

            if (this.cellstyleHeader == null)
            {
                cellstyleHeader = workbook.CreateCellStyle();
                cellstyleHeader.SetFont(fontHeader);
                cellstyleHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            }

            if (this.font == null)
            {
                font = workbook.CreateFont();
                font.Boldweight = (short)FontBoldWeight.Normal;
                font.FontName = "Verdana";
                font.FontHeightInPoints = 10;
            }

            if (this.fontBold == null)
            {
                fontBold = workbook.CreateFont();
                fontBold.Boldweight = (short)FontBoldWeight.Bold;
                fontBold.FontName = "Verdana";
                fontBold.FontHeightInPoints = 10;
            }

            // DETAIL row
            // normal styles
            if (this.cellstyle == null)
            {
                cellstyle = workbook.CreateCellStyle();
                cellstyle.SetFont(font);
            }
            if (this.cellstyleInt == null)
            {
                cellstyleInt = workbook.CreateCellStyle();
                cellstyleInt.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0");
                cellstyleInt.SetFont(font);
            }
            if (this.cellstyleIntNoSep == null)
            {
                cellstyleIntNoSep = workbook.CreateCellStyle();
                cellstyleIntNoSep.DataFormat = HSSFDataFormat.GetBuiltinFormat("#0");
                cellstyleIntNoSep.SetFont(font);
            }
            if (this.cellstyleDec == null)
            {
                cellstyleDec = workbook.CreateCellStyle();
                cellstyleDec.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
                cellstyleDec.SetFont(font);
            }
            if (this.cellstyleDate == null)
            {
                cellstyleDate = workbook.CreateCellStyle();
                cellstyleDate.DataFormat = workbook.CreateDataFormat().GetFormat("dd/mm/yyyy");
                cellstyleDate.SetFont(font);
            }
            if (this.cellstylePercent == null)
            {
                cellstylePercent = workbook.CreateCellStyle();
                cellstylePercent.DataFormat = workbook.CreateDataFormat().GetFormat("0.00%");
                cellstylePercent.SetFont(font);
            }

            // highlight styles
            if (this.cellstyleHL == null)
            {
                cellstyleHL = workbook.CreateCellStyle();
                cellstyleHL.SetFont(font);
                cellstyleHL.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHL.FillPattern = FillPattern.SolidForeground;
                cellstyleHL.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLInt == null)
            {
                cellstyleHLInt = workbook.CreateCellStyle();
                cellstyleHLInt.SetFont(font);
                cellstyleHLInt.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0");
                cellstyleHLInt.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLInt.FillPattern = FillPattern.SolidForeground;
                cellstyleHLInt.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLIntNoSep == null)
            {
                cellstyleHLIntNoSep = workbook.CreateCellStyle();
                cellstyleHLIntNoSep.DataFormat = HSSFDataFormat.GetBuiltinFormat("#0");
                cellstyleHLIntNoSep.SetFont(font);
                cellstyleHLIntNoSep.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLIntNoSep.FillPattern = FillPattern.SolidForeground;
                cellstyleHLIntNoSep.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLDec == null)
            {
                cellstyleHLDec = workbook.CreateCellStyle();
                cellstyleHLDec.SetFont(font);
                cellstyleHLDec.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
                cellstyleHLDec.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLDec.FillPattern = FillPattern.SolidForeground;
                cellstyleHLDec.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLDate == null)
            {
                cellstyleHLDate = workbook.CreateCellStyle();
                cellstyleHLDate.SetFont(font);
                cellstyleHLDate.DataFormat = workbook.CreateDataFormat().GetFormat("dd/mm/yyyy");
                cellstyleHLDate.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLDate.FillPattern = FillPattern.SolidForeground;
                cellstyleHLDate.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLPercent == null)
            {
                cellstyleHLPercent = workbook.CreateCellStyle();
                cellstyleHLPercent.SetFont(font);
                cellstyleHLPercent.DataFormat = workbook.CreateDataFormat().GetFormat("0.00%");
                cellstyleHLPercent.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLPercent.FillPattern = FillPattern.SolidForeground;
                cellstyleHLPercent.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }

            // highlight + bold styles
            if (this.cellstyleHLBold == null)
            {
                cellstyleHLBold = workbook.CreateCellStyle();
                cellstyleHLBold.SetFont(fontBold);
                cellstyleHLBold.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLBold.FillPattern = FillPattern.SolidForeground;
                cellstyleHLBold.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLBoldInt == null)
            {
                cellstyleHLBoldInt = workbook.CreateCellStyle();
                cellstyleHLBoldInt.SetFont(fontBold);
                cellstyleHLBoldInt.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0");
                cellstyleHLBoldInt.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLBoldInt.FillPattern = FillPattern.SolidForeground;
                cellstyleHLBoldInt.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLBoldIntNoSep == null)
            {
                cellstyleHLBoldIntNoSep = workbook.CreateCellStyle();
                cellstyleHLBoldIntNoSep.DataFormat = HSSFDataFormat.GetBuiltinFormat("#0");
                cellstyleHLBoldIntNoSep.SetFont(fontBold);
                cellstyleHLBoldIntNoSep.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLBoldIntNoSep.FillPattern = FillPattern.SolidForeground;
                cellstyleHLBoldIntNoSep.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLBoldDec == null)
            {
                cellstyleHLBoldDec = workbook.CreateCellStyle();
                cellstyleHLBoldDec.SetFont(fontBold);
                cellstyleHLBoldDec.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
                cellstyleHLBoldDec.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLBoldDec.FillPattern = FillPattern.SolidForeground;
                cellstyleHLBoldDec.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLBoldDate == null)
            {
                cellstyleHLBoldDate = workbook.CreateCellStyle();
                cellstyleHLBoldDate.SetFont(fontBold);
                cellstyleHLBoldDate.DataFormat = workbook.CreateDataFormat().GetFormat("dd/mm/yyyy");
                cellstyleHLBoldDate.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLBoldDate.FillPattern = FillPattern.SolidForeground;
                cellstyleHLBoldDate.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLBoldPercent == null)
            {
                cellstyleHLBoldPercent = workbook.CreateCellStyle();
                cellstyleHLBoldPercent.SetFont(fontBold);
                cellstyleHLBoldPercent.DataFormat = workbook.CreateDataFormat().GetFormat("0.00%");
                cellstyleHLBoldPercent.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
                cellstyleHLBoldPercent.FillPattern = FillPattern.SolidForeground;
                cellstyleHLBoldPercent.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }

            // highlight styles in RED
            if (this.cellstyleHLRed == null)
            {
                cellstyleHLRed = workbook.CreateCellStyle();
                cellstyleHLRed.SetFont(font);
                cellstyleHLRed.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
                cellstyleHLRed.FillPattern = FillPattern.SolidForeground;
                cellstyleHLRed.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLRedInt == null)
            {
                cellstyleHLRedInt = workbook.CreateCellStyle();
                cellstyleHLRedInt.SetFont(font);
                cellstyleHLRedInt.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0");
                cellstyleHLRedInt.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
                cellstyleHLRedInt.FillPattern = FillPattern.SolidForeground;
                cellstyleHLRedInt.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLRedIntNoSep == null)
            {
                cellstyleHLRedIntNoSep = workbook.CreateCellStyle();
                cellstyleHLRedIntNoSep.DataFormat = HSSFDataFormat.GetBuiltinFormat("#0");
                cellstyleHLRedIntNoSep.SetFont(font);
                cellstyleHLRedIntNoSep.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
                cellstyleHLRedIntNoSep.FillPattern = FillPattern.SolidForeground;
                cellstyleHLRedIntNoSep.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLRedDec == null)
            {
                cellstyleHLRedDec = workbook.CreateCellStyle();
                cellstyleHLRedDec.SetFont(font);
                cellstyleHLRedDec.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
                cellstyleHLRedDec.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
                cellstyleHLRedDec.FillPattern = FillPattern.SolidForeground;
                cellstyleHLRedDec.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLRedDate == null)
            {
                cellstyleHLRedDate = workbook.CreateCellStyle();
                cellstyleHLRedDate.SetFont(font);
                cellstyleHLRedDate.DataFormat = workbook.CreateDataFormat().GetFormat("dd/mm/yyyy");
                cellstyleHLRedDate.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
                cellstyleHLRedDate.FillPattern = FillPattern.SolidForeground;
                cellstyleHLRedDate.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
            if (this.cellstyleHLRedPercent == null)
            {
                cellstyleHLRedPercent = workbook.CreateCellStyle();
                cellstyleHLRedPercent.SetFont(font);
                cellstyleHLRedPercent.DataFormat = workbook.CreateDataFormat().GetFormat("0.00%");
                cellstyleHLRedPercent.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
                cellstyleHLRedPercent.FillPattern = FillPattern.SolidForeground;
                cellstyleHLRedPercent.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            }
        }
    }
}
